/**
 * Created by qiang on 2017-04-26 .
 */
var self;
var vm = new Vue({
	el: '#main',
	data: {
		rechargeOptions: [],
		selectItem: null,
		userInfo: null,
    showMask: false
	},
	mounted: function() {
		self = this;
		common.auth(function() {
			if (sessionStorage.getItem("IsNewUser") != 'T') {
				self.getUserDetail()
				self.getRechageOprions()
			} else {
                self.getRechageOprions()
			}
		});
        common.YMCount();
	},
	computed: {
		addNum: function() {
			if (!this.userInfo) return 0;
			return this.userInfo.PaidAddNum;
		}
	},
	methods: {
		getUserDetail: function() {
			service.getUserDetail({}, function(data) {
				console.log(data)
				if (data && data.userInfo)  {
					self.userInfo = data.userInfo;
				}
			})
		},
		selectRechargeItem: function(item) {
			self.selectItem = item;
		},
		getRechageOprions: function() {
			var getRechageOprionsCallBack = function (res) {
				console.log(res);
				self.rechargeOptions = res;
				if (res.length > 0) {
					self.selectItem = res[0];
				}
				$(".loading-bg").hide();
			}
			service.getRechageOptionList(getRechageOprionsCallBack)
		},
		pay: function(text, event) {
            if (sessionStorage.getItem("IsNewUser") === 'T') {
                weui.alert("请先完成注册", function() {
                    location.href = 'userInfo.html';
                })
                return;
            }
			console.log(event)
			var createWXPayCallBack = function (res) {
//				alert(JSON.stringify(res))
				WeixinJSBridge.invoke('getBrandWCPayRequest', {
					appId: res.appId,
					timeStamp: res.timeStamp, // 支付签名时间戳，注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
					nonceStr: res.nonceStr, // 支付签名随机串，不长于 32 位
					package: res.package, // 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=***）
					signType: res.signType, // 签名方式，默认为'SHA1'，使用新版支付需传入'MD5'
					paySign: res.paySign // 支付签名
				}, function (res) {
                    // 支付成功后的回调函数
                  //  console.log(res)
                    self.getUserDetail()
                });
			};
			var options = {
				RechargeTotalFee: self.selectItem.Price,
				AddTimes: self.selectItem.AddTimes
			};
			service.createWXPay(options, createWXPayCallBack);
		}
	}
})
